package sweet.helper.cli

/** Similar to scala.Application, but with better option parsing and args support. 
 * You may mix/entends this trait with #run method and get immediately access to args and opts variables.
 * For example: <pre><code>
 *    object MyApp extends sweet.helper.cli.CliApp{
 *      def run{
 *        if(opts.contains("h")){ println("scala MyApp [options] arguments."); exit(1) }
 *        
 *        for(x <- args) println("Argument " + x)
 *        for((k,v) <- opts) println("Options " + k + ": " + v)
 *      }
 *    }
 * </code></pre>
 * You may now test your MyApp like this:
 * <pre>$ sweet/bin/run.sh MyApp</pre>
 * <pre>$ sweet/bin/run.sh MyApp -h</pre>
 * <pre>$ sweet/bin/run.sh MyApp one two three</pre>
 * <pre>$ sweet/bin/run.sh MyApp -v -f -b one two three</pre>
 */
trait CliApp extends OptionsParser {
  /** Holds the new argument list after parseOpt */
  var args: List[String] = _
  
  /** Holds the options map after parseOpt */
  var opts: scala.collection.Map[String, String] = _
  
  /** default implementation that will parse argv and initialize new args and opts.*/
  def main(argv: Array[String]){
    val res = parseOpts(argv)
    args = res._1
    opts = res._2
    run
  }
  
  /** Subclass need to implement this to get access to args and opts. */
  def run: Unit
}
